<template>
  <div class="search-phones">
    <div class="set-parameters">
      <el-input placeholder="请输入手机号码" v-model="phonenumber" maxlength="20">
        <template slot="prepend">手机号码</template>
      </el-input>
      <el-input placeholder="请输入size" v-model="pageSize" maxlength="9">
        <template slot="prepend">页面大小</template>
      </el-input>
      <el-input placeholder="请输入size" v-model="pageNo" maxlength="5">
        <template slot="prepend">页面号码</template>
      </el-input>
      <!--  -->
      <el-row type="flex">
        <div class="time-tip">时间段</div>
        <el-date-picker
          v-model="value1"
          type="datetimerange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="['12:00:00']"
        ></el-date-picker>
      </el-row>

      <el-button type="primary" @click="searchPhones">开始搜索</el-button>
    </div>

    <!-- 号码列表 -->
    <el-table
      :data="tableData[currentPage-1]"
      stripe
      border
      height="auto"
      class="table-data"
      style="width: 100%"
    >
      <el-table-column label="id" prop="id" width="100px"></el-table-column>
      <el-table-column label="微信号" prop="wechatid" width="170px"></el-table-column>
      <el-table-column label="微信昵称" width="170px">
        <template slot-scope="scope">
          <span
            v-if="wechatsMap[scope.row.wechatid]"
            v-text="wechatsMap[scope.row.wechatid].WeChatNick"
          ></span>
          <span v-else>{{scope.row.wechatid}}</span>
        </template>
      </el-table-column>
      <el-table-column label="号码" prop="phonenumber"></el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          <span v-text="$options.filters.timeFilter(scope.row.createTime)"></span>
        </template>
      </el-table-column>
      <el-table-column label="号码是否有效">
        <template slot-scope="scope">
          <span style="color:#67c23a;" v-if="scope.row.task_result === 0">有效</span>
          <span style="color:#f56c6c;" v-else-if="scope.row.task_result === 1">无效</span>
          <span style="color:#909399;" v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column label="结果">
        <template slot-scope="scope">
          <span v-text="scope.row.state === 0 ? '已完成':'未执行'"></span>
        </template>
      </el-table-column>
    </el-table>

    <div class="data-count">
      <span>总的号码数：{{phonesList.totalSize}}</span>
      <span>添加失败的号码数：{{phonesList.addFailSize}}</span>
      <span>没有用户号码数：{{phonesList.notUseSize}}</span>
    </div>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[100, 200, 300, 500]"
      :page-size="size"
      :pager-count="5"
      layout="total, sizes, prev, pager, next, jumper"
      :total="phonesList.list ? phonesList.list.length : 0"
    ></el-pagination>
  </div>
</template>

<script>
import { mapGetters, mapActions } from 'vuex'

export default {
  name: 'ToolsSearchPhones',
  data() {
    return {
      pageSize: 100, // 每页几个数据
      pageNo: 1, // 每页几个数据
      size: 100, // 当前页
      currentPage: 1, // 当前页
      phonenumber: '',
      value1: '', // 当前页
    }
  },
  computed: {
    ...mapGetters({
      phonesList: 'nedb/phonesList',
      wechats: "conversation/wechats",

    }),
    tableData() {
      let td = []
      if (this.phonesList.list) {
        this.phonesList.list.forEach((e) => {
          td.push(e)
        })
      }
      td.sort((a, b) => {
        return a.id - b.id
      })

      let tdArry = []
      let size = this.size
      if (td.length > 0) {
        for (let i = 0; i < td.length; i += size) {
          tdArry.push(td.slice(i, i + size))
        }
      }
      return tdArry
    },
    wechatsMap() {
      let wm = {}
      this.wechats.forEach((e) => {
        wm[e.WeChatId] = e
      })
      return wm
    },
  },
  methods: {
    // 修改pageSize
    handleSizeChange(val) {
      if (this.size === val) return
      this.size = val
    },
    // 修改当前页
    handleCurrentChange(val) {
      this.currentPage = val
    },
    // 时间戳转换成 2020-09-21 16:44
    transformTime(times) {
      var date = new Date(times)
      let Y = date.getFullYear() + '-'
      let M =
        (date.getMonth() + 1 < 10
          ? '0' + (date.getMonth() + 1)
          : date.getMonth() + 1) + '-'
      let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
      let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
      let m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
      // let s = date.getSeconds()
      let time = Y + M + D + h + m
      return time
    },
    // 查询号码
    searchPhones() {
      let sc = {
        phonenumber: this.phonenumber,
        begin_time: this.value1[0] ? this.transformTime(this.value1[0]) : '',
        end_time: this.value1[1] ? this.transformTime(this.value1[1]) : '',
        pageNo: this.pageNo,
        pageSize: this.pageSize,
      }
      this.$store.dispatch("websocket/GetPhoneNumberList", sc)
    },
  }
}
</script>

<style lang="scss" scoped>
.search-phones {
  height: 100%;
  display: flex;
  flex-direction: column;

  .set-parameters {
    padding: 5px;
    position: relative;
    border-bottom: solid $border-color-1 2px;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    .el-input {
      width: 300px;
      margin: 10px 20px 10px 0;
    }
    .time-tip {
      background-color: #f5f7fa;
      color: #909399;
      width: 97px;
      height: 40px;
      line-height: 40px;
      vertical-align: middle;
      display: table-cell;
      border-radius: 4px;
      padding: 0 20px;
      white-space: nowrap;
      border: 1px solid #dcdfe6;
    }
    .el-button {
      margin-left: 20px;
    }
  }

  .table-data {
    flex: 1 1 auto;
  }

  .data-count {
    padding: 5px;
    span {
      margin-right: 10px;
    }
  }

  .el-pagination {
    padding: 5px;
    display: flex;
    justify-content: center;
    align-items: center;
    border-top: solid 1px $border-color-1;
    background-color: white;
  }
}
</style>
